package jpave.query_interface;

/**************************************************************
 * Top line of Basic DB Hits Query and Basic Sequence Query
 */
import java.awt.Color;
import java.awt.Component;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

import util.Debug.ErrorReport;
import util.ui.UserPrompt;

import jpave.dataholders.BasicQueryData;
import jpave.manager.ManagerFrame;

public class BasicQueryTab extends Tab {
	private static final long serialVersionUID = -5515249017308285183L;

	public static final int VIEW_BY_HIT = 1; // default 
	public static final int VIEW_BY_SEQ = 2;
	
	public static final String VIEW_CONTIG_LABEL = "View Selected Sequence";
	public static final String VIEW_HIT_LABEL = "View sequences for selected Hit"; // CAS 12/21/14
	public static final String ALIGN_CONTIG_LABEL = "Align Selected Sequence";
	public static final String EXPORT_LABEL = "Export";
	public static final String COPY_TABLE_LABEL = "Copy Table";
	private static final Color BGCOLOR = ManagerFrame.BGCOLOR;

	public BasicQueryTab(JPaveFrame parentFrame, int type, BasicQueryData data) {
		super(parentFrame, null);
		setBackground(BGCOLOR);
		theData = data;
		nType = type;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		createButtonPanel();
		createPromptPanel();
		add(thePromptPanel);
		
		theResultListPanel = new BasicQueryResultListPanel(this, parentFrame.getAssemblyList());
		add(theResultListPanel);
	}
	
	public void createButtonPanel() {
		btnViewContigs = (nType == VIEW_BY_SEQ ? new JButton(VIEW_CONTIG_LABEL) : new JButton(VIEW_HIT_LABEL));
		btnViewContigs.setBackground(ManagerFrame.FUNCTIONCOLOR);
		btnViewContigs.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				viewContigsInTable();
			}
		});
		btnViewContigs.setEnabled(false);
		
		btnExport = new JButton(EXPORT_LABEL);
		btnExport.setBackground(ManagerFrame.PROMPTCOLOR);
		btnExport.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				theResultListPanel.exportTableToFile();
			}
		});
		btnExport.setEnabled(false);
		
		btnCopyTable = new JButton(COPY_TABLE_LABEL);
		btnCopyTable.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
			try {
				Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
				cb.setContents(new StringSelection(theResultListPanel.createTableString("\t")), null);
			} catch (Exception e) {							
				ErrorReport.reportError(e, "Error copying table");
			} catch (Error e) {
				ErrorReport.reportFatalError(e, "Fatal error copying table", null);
			}
		}
	
		});
		btnCopyTable.setEnabled(false);
		
		btnHelp = new JButton("Help");
		btnHelp.setBackground(ManagerFrame.HELPCOLOR);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(nType == VIEW_BY_HIT)
					UserPrompt.displayHTMLResourceHelp(getParentFrame(), 
							"Basic DB Hits Query", "html/viewSingleTCW/BasicQueryHit.html");
				else
					UserPrompt.displayHTMLResourceHelp(getParentFrame(),
							"Basic Sequence Query", "html/viewSingleTCW/BasicQueryContig.html");
			}
		});

		buttonPanel = new JPanel();
		buttonPanel.setBackground(BGCOLOR);
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
		buttonPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
		
		buttonPanel.add(btnViewContigs);
		buttonPanel.add(Box.createHorizontalStrut(10));
		buttonPanel.add(btnExport);
		buttonPanel.add(Box.createHorizontalStrut(10));
		buttonPanel.add(btnCopyTable);
		buttonPanel.add(Box.createHorizontalStrut(10));
		buttonPanel.add(btnHelp);
		
		add(Box.createVerticalStrut(5));		
		add(buttonPanel);
		add(Box.createVerticalStrut(15));		
	}
	public String getHitSummary() { return thePromptPanel.getHitSearchSummary(); } 
	
	public String getContigIDAtRow(int newRow) {
		return theResultListPanel.getContigAt(newRow);
	}
	
	public int getPrevRowNum(int row) { 
		return theResultListPanel.getPrevUniqueContig(row); 
	}
	
	public int getNextRowNum(int row) { 
		return theResultListPanel.getNextUniqueContig(row); 
	}
	
	public void setViewContigEnabled(boolean enabled) {
		btnViewContigs.setEnabled(enabled);
	}
	
	public void setExportEnabled(boolean enabled) {
		btnExport.setEnabled(enabled);
	}
	
	public void setCopyTableEnabled(boolean enabled) {
		btnCopyTable.setEnabled(enabled);
	}
	
	public void update() {
		thePromptPanel.updateViewMode();
		theResultListPanel.refreshTable();
	}
	
	public int getResultLimit() { return thePromptPanel.getResultsLimit(); } 
	public void addListFromQuery() { theResultListPanel.addListFromQuery(); }
	public String getSearchStr() { return thePromptPanel.getSearchStr(); }
	public void setAddToTableEnabled(boolean enabled) { thePromptPanel.setAddToTableEnabled(enabled); }
	
	public String [] getSelectedSpecies() { return thePromptPanel.getSpeciesSelected(); }
	public boolean isSpeciesInclude() { return thePromptPanel.isSpeciesInclude(); }
	
	public int getRankLimit() { return thePromptPanel.getRankLimit(); }
	public double getEValLimit() { return thePromptPanel.getEValLimit(); }
	public boolean getBestAnno() {return thePromptPanel.isBestAnnoSelected(); }
	public boolean getBestEval() {return thePromptPanel.isBestEvalSelected(); }
	
	public boolean isKEGGSelected() { return thePromptPanel.isKEGGSelected(); }
	public boolean isPFAMSelected() { return thePromptPanel.isPFAMSelected(); }
	public boolean isECSelected() { return thePromptPanel.isECSelected(); }
	
	public String getKEGGSubStr() { return thePromptPanel.getKEGGSubStr(); }
	public String getPFAMSubStr() { return thePromptPanel.getPFAMSubStr(); }
	public String getECSubStr() { return thePromptPanel.getECSubStr(); }
	
	public int getQueryMode() { return thePromptPanel.getQueryMode(); }
	public boolean isGroupedSelected() { return thePromptPanel.isGroupedSelected(); }
	
	public String [] getColumns() { return thePromptPanel.getAllColumnNames(); }
	public boolean [] getColumnSelections() { return thePromptPanel.getAllColumnSelections(); }
	
	public void hideAll() {
		buttonPanel.setVisible(false);
		theResultListPanel.setVisible(false);
		getParentFrame().setButtonsVisible(false);
	}
	
	public void showAll() {
		buttonPanel.setVisible(true);
		theResultListPanel.setVisible(true);
	}
	
	public void viewContigsInTable() {
		String [] contigNames = theResultListPanel.getSelectedContigs();
		String [] hitIDs = theResultListPanel.getSelectedHitIDs();
		
	    String tag=null; 
	    
		int viewMode = JPaveFrame.BASIC_QUERY_MODE_HIT;
		if(nType == VIEW_BY_SEQ) {
			viewMode = JPaveFrame.BASIC_QUERY_MODE_UNITRANS;
		}
		else {
			if(hitIDs.length > 1)
				tag = hitIDs.length + " Hits";
			else
				tag = hitIDs[0];
		}
		getParentFrame().loadContigs(tag, contigNames, viewMode );
	}
	
	public int getType() { return nType; }
	public boolean isTypeContig() { return nType == VIEW_BY_SEQ; }
	
	public boolean isGroupSeleced() { return thePromptPanel.isGroupedSelected(); }
	
	public String [] getLibraryNames() { return thePromptPanel.getLibraryNames(); }
	public String [] getContigSetNames() { return thePromptPanel.getContigSetNames(); }
	
	public String[] getAnnoDBs() { return getParentFrame().getAnnoDBs(); }
	public Vector<String> getSpecies() { return getParentFrame().getSpecies(); }
	
	public String [] getAnnoTypes() { return thePromptPanel.getAnnoTypes(); }
	public String [] getAnnoTaxo() { return thePromptPanel.getAnnoTaxo(); }
	
	private void createPromptPanel() {
		thePromptPanel = new BasicPromptPanel(this, nType);
		thePromptPanel.setBackground(BGCOLOR);
		
		ActionListener showListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showAll();
			}
		};
		
		ActionListener updateListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				update();
				showAll();
			}
		};
		
		ActionListener hideListener = new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				hideAll();
			}
		};
		
		thePromptPanel.addColumnSelectShowListener(showListener);
		thePromptPanel.addAnnoDBShowListener(showListener);
		thePromptPanel.addSpeciesShowListener(showListener);
		thePromptPanel.addAttributeShowListener(showListener);
		
		thePromptPanel.addColumnSelectUpdateListener(updateListener);
		thePromptPanel.addAnnoDBUpdateListener(updateListener);
		thePromptPanel.addSpeciesUpdateListener(updateListener);
		thePromptPanel.addAttributeUpdateListener(updateListener);

		thePromptPanel.addColumnSelectHideListener(hideListener);
		thePromptPanel.addAnnoDBHideListener(hideListener);
		thePromptPanel.addSpeciesHideListener(hideListener);
		thePromptPanel.addAttributeHideListener(hideListener);
	}
	
	public void setBuriedReadVisible(boolean visible) {
		if(theResultListPanel != null)
		theResultListPanel.setShowBuriedVisible(visible);
	}

	private int nType = -1;
	private JButton btnViewContigs = null;
	private JButton btnExport = null;
	private JButton btnCopyTable = null;
	private JButton btnHelp = null;
	private JPanel buttonPanel = null;
	private BasicQueryData theData = null;
	private BasicPromptPanel thePromptPanel = null;
	private BasicQueryResultListPanel theResultListPanel = null;
}
